package com.zhiqianbao.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.zhiqianbao.R;
import com.zhiqianbao.shared.SharedPreferanceUtils;
import com.zhiqianbao.utils.Constants;
import com.zhiqianbao.utils.WorthPurseUtil;

import java.util.ArrayList;
import java.util.List;

import static com.zhiqianbao.shared.SharedPreferanceUtils.initSharedData;

/**
 * Created by liang on 2016/1/27.
 */
public class ApplyTabHostActivity extends BaseActivity implements View.OnClickListener{

    private static String TAG = ApplyTabHostActivity.class.getSimpleName();
    ViewPagerAdapter adapter;
    TextView applyLoapTv;
    TextView applyActivityTv;
    TextView applyProgressTv;
    ViewPager pager;
    private SharedPreferanceUtils sharedPreferanceUtils;
    private List<Fragment> fragments = new ArrayList<>();
    int selPosition = 0;
    private int lg_frag;
    private List<TextView> tvLists=new ArrayList<>();
    private int userloginflag=0;//判断页面回调是否跳转进度查询模块
    private void initFragment() {
        tvLists.add(applyLoapTv);
        tvLists.add(applyActivityTv);
        tvLists.add(applyProgressTv);
        fragments.add(new ApplyLoanFragment());
        fragments.add(new ApplyActiveFragmentNew());
        fragments.add(new ApplyProgressFragment());
        pager.setOffscreenPageLimit(0);
        adapter = new ViewPagerAdapter(getSupportFragmentManager(), fragments);
        pager.setAdapter(adapter);
//        pager.setOffscreenPageLimit(3);
        pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                if (position == 2) {
                    //进度查询必须是登录用户，未登录跳转登录页面
                    if (Constants.LOGIN_USER == null) {
                        Intent loginIntent = new Intent(ApplyTabHostActivity.this,
                                WorthPurseUtil.getZhiddActivity().getClass());
                        startActivity(loginIntent);
                        showShortToast1("您的账号在其他设备登录,请重新登录");
                        finish();
                    } else {
                        tabTextClolrBg(position);
                        selPosition=position;
                    }
                } else {
                    tabTextClolrBg(position);
                    selPosition=position;
                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_tabhost);
        initView();
        initSharedData(this);
        sharedPreferanceUtils = new SharedPreferanceUtils(this);
        initFragment();
//        String lg_frag = getIntent().getExtras().getString("lg_frag");
//        if (lg_frag.equals(5)){
//            //跳转到活动优惠
//            onTabSelected(1);
//        }
    }
    public void initView(){
        applyLoapTv=(TextView)findViewById(R.id.apply_loap_tv);
        applyActivityTv=(TextView)findViewById(R.id.apply_activity_tv);
        applyProgressTv=(TextView)findViewById(R.id.apply_progress_tv);
        applyLoapTv.setOnClickListener(this);
        applyActivityTv.setOnClickListener(this);
        applyProgressTv.setOnClickListener(this);
        pager=(ViewPager) findViewById(R.id.pager);
    }
    @Override
    protected void onResume() {
        super.onResume();
        int ids = getIntent().getIntExtra("userloginflag", 0);
        lg_frag =  getIntent().getIntExtra("lg_frag",0);
        if(ids==1){
//                pager.setCurrentItem(0);
            onTabSelected(2);
        }
        if (lg_frag==5){
            onTabSelected(1);
        }

        //需要测试下借款结束是否会到进度查询页面
//        if(userloginflag!=1) {
//            pager.setCurrentItem(0);
//        }
//        userloginflag=0;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
       if(R.id.apply_loap_tv==v.getId()) {
               onTabSelected(0);
       }
       else if(R.id.apply_activity_tv==v.getId()) {
           onTabSelected(1);
       }
        else if(R.id.apply_progress_tv==v.getId()){
                onTabSelected(2);
        }
    }
    public void onTabSelected(int position) {
        if (position != selPosition) {
            if (position == 2) {
                //进度查询必须是登录用户，未登录跳转登录页面
                if (Constants.LOGIN_USER == null) {
                    Intent loginIntent = new Intent(ApplyTabHostActivity.this,
                            WorthPurseUtil.getZhiddActivity().getClass());
                    startActivity(loginIntent);
                    showShortToast1("您的账号在其他设备登录,请重新登录");
                    finish();
                } else {
                    pager.setCurrentItem(position);
                }
            } else {
                pager.setCurrentItem(position);
            }
        }
    }

    public void tabTextClolrBg(int position){
        if(selPosition!=position){
            tvLists.get(selPosition).setBackgroundColor(Color.WHITE);
            tvLists.get(selPosition).setTextColor(Color.parseColor("#ffc80000"));
            tvLists.get(position).setBackgroundResource(R.drawable.apply_tab_bgcolor);
            tvLists.get(position).setTextColor(Color.parseColor("#c80000"));
        }
    }

    public class ViewPagerAdapter extends FragmentPagerAdapter {
        private Fragment mCurFragment;
        private List<Fragment> viewPagerFragments;

        public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.viewPagerFragments = fragments;
        }

        @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
            super.setPrimaryItem(container, position, object);
            if (object instanceof Fragment) {
                mCurFragment = (Fragment) object;
            }
        }

        public Fragment getCurFragment() {
            return mCurFragment;
        }

        @Override
        public Fragment getItem(int position) {
            return Fragment.instantiate(getApplicationContext(), viewPagerFragments
                    .get(position).getClass().getName(), null);
        }

        @Override
        public int getCount() {
            return viewPagerFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "";
        }

        @Override
        public int getItemPosition(Object object) {
            return PagerAdapter.POSITION_NONE;
        }

    }

    long mExitTime;

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                Object mHelperUtils;
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);//必须退出应用
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case RESULT_OK:
                userloginflag = data.getIntExtra("userloginflag", 0);
                if (userloginflag == 1) {
                    pager.setCurrentItem(2);
                }
                break;
        }
    }
}
